<template>
  <view 
    v-if="visible" 
    class="base-modal-overlay"
    :class="{ 'fade-in': visible }"
    :style="overlayStyle"
    @click="handleMaskClick"
  >
    <view 
      class="base-modal-container"
      :class="[
        `position-${position}`,
        `animation-${animation}`,
        { 'modal-show': visible }
      ]"
      :style="containerStyle"
      @click.stop
    >
      <!-- 弹窗头部 -->
      <view v-if="showHeader" class="modal-header">
        <text class="modal-title">{{ title }}</text>
        <button 
          v-if="showClose" 
          class="close-btn" 
          @click="handleClose"
        >
          <text class="close-icon">✕</text>
        </button>
      </view>
      
      <!-- 弹窗内容 -->
      <view class="modal-body" :style="bodyStyle">
        <slot></slot>
      </view>
      
      <!-- 弹窗底部（可选） -->
      <view v-if="$slots.footer || showFooter" class="modal-footer">
        <slot name="footer">
          <button 
            v-if="showCancel" 
            class="footer-btn cancel-btn" 
            @click="handleCancel"
          >
            {{ cancelText }}
          </button>
          <button 
            v-if="showConfirm" 
            class="footer-btn confirm-btn" 
            @click="handleConfirm"
          >
            {{ confirmText }}
          </button>
        </slot>
      </view>
    </view>
  </view>
</template>

<script>
import modalManager from '../utils/modal-manager.js'

export default {
  name: 'BaseModal',
  
  props: {
    // 显示状态
    visible: {
      type: Boolean,
      default: false
    },
    
    // 弹窗标题
    title: {
      type: String,
      default: ''
    },
    
    // 弹窗宽度
    width: {
      type: String,
      default: '90%'
    },
    
    // 弹窗高度
    height: {
      type: String,
      default: 'auto'
    },
    
    // 最大宽度
    maxWidth: {
      type: String,
      default: '600rpx'
    },
    
    // 最大高度
    maxHeight: {
      type: String,
      default: '80vh'
    },
    
    // 显示位置 (center/top/bottom)
    position: {
      type: String,
      default: 'center',
      validator: (value) => ['center', 'top', 'bottom'].includes(value)
    },
    
    // 动画类型 (fade/slide/zoom)
    animation: {
      type: String,
      default: 'fade',
      validator: (value) => ['fade', 'slide', 'zoom', 'none'].includes(value)
    },
    
    // 点击遮罩是否关闭
    maskClosable: {
      type: Boolean,
      default: true
    },
    
    // 显示关闭按钮
    showClose: {
      type: Boolean,
      default: true
    },
    
    // 显示头部
    showHeader: {
      type: Boolean,
      default: true
    },
    
    // 显示底部
    showFooter: {
      type: Boolean,
      default: false
    },
    
    // 显示取消按钮
    showCancel: {
      type: Boolean,
      default: false
    },
    
    // 显示确认按钮
    showConfirm: {
      type: Boolean,
      default: false
    },
    
    // 取消按钮文本
    cancelText: {
      type: String,
      default: '取消'
    },
    
    // 确认按钮文本
    confirmText: {
      type: String,
      default: '确认'
    },
    
    // 优先级 (normal/important/critical/system)
    priority: {
      type: String,
      default: 'normal',
      validator: (value) => ['normal', 'important', 'critical', 'system'].includes(value)
    },
    
    // 自定义z-index（不推荐，优先使用priority）
    zIndex: {
      type: Number,
      default: null
    },
    
    // 毛玻璃效果
    blur: {
      type: Boolean,
      default: true
    },
    
    // 背景颜色
    backgroundColor: {
      type: String,
      default: '#ffffff'
    },
    
    // 圆角
    borderRadius: {
      type: String,
      default: '20rpx'
    }
  },
  
  data() {
    return {
      modalId: `modal_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
      computedZIndex: 1000
    }
  },
  
  computed: {
    // 遮罩样式
    overlayStyle() {
      return {
        zIndex: this.computedZIndex
      }
    },
    
    // 容器样式
    containerStyle() {
      return {
        width: this.width,
        height: this.height,
        maxWidth: this.maxWidth,
        maxHeight: this.maxHeight,
        backgroundColor: this.backgroundColor,
        borderRadius: this.borderRadius
      }
    },
    
    // 内容区域样式
    bodyStyle() {
      return {
        maxHeight: this.showHeader ? 'calc(100% - 100rpx)' : '100%'
      }
    }
  },
  
  watch: {
    visible(newVal) {
      if (newVal) {
        this.registerModal()
      } else {
        this.unregisterModal()
      }
    }
  },
  
  mounted() {
    if (this.visible) {
      this.registerModal()
    }
  },
  
  beforeUnmount() {
    this.unregisterModal()
  },
  
  methods: {
    // 注册弹窗
    registerModal() {
      if (this.zIndex !== null) {
        // 使用自定义 z-index
        this.computedZIndex = this.zIndex
      } else {
        // 使用管理器分配的 z-index
        this.computedZIndex = modalManager.register(this.modalId, this.priority)
      }
    },
    
    // 注销弹窗
    unregisterModal() {
      if (this.zIndex === null) {
        modalManager.unregister(this.modalId)
      }
    },
    
    // 处理遮罩点击
    handleMaskClick() {
      if (this.maskClosable) {
        this.handleClose()
      }
    },
    
    // 关闭弹窗
    handleClose() {
      this.$emit('update:visible', false)
      this.$emit('close')
    },
    
    // 取消操作
    handleCancel() {
      this.$emit('cancel')
      this.handleClose()
    },
    
    // 确认操作
    handleConfirm() {
      this.$emit('confirm')
    }
  }
}
</script>

<style scoped>
/* 遮罩层 */
.base-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40rpx;
  backdrop-filter: blur(4rpx);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.base-modal-overlay.fade-in {
  opacity: 1;
}

/* 弹窗容器 */
.base-modal-container {
  background: white;
  border-radius: 20rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  max-width: 100%;
  max-height: 100%;
  transform-origin: center center;
}

/* 位置 - 居中 */
.base-modal-container.position-center {
  /* 默认位置 */
}

/* 位置 - 顶部 */
.base-modal-container.position-top {
  align-self: flex-start;
  margin-top: 0;
}

/* 位置 - 底部 */
.base-modal-container.position-bottom {
  align-self: flex-end;
  margin-bottom: 0;
  border-radius: 20rpx 20rpx 0 0;
}

/* 动画 - 淡入淡出 */
.base-modal-container.animation-fade {
  opacity: 0;
  transition: opacity 0.3s ease;
}

.base-modal-container.animation-fade.modal-show {
  opacity: 1;
}

/* 动画 - 滑入滑出 */
.base-modal-container.animation-slide {
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.base-modal-container.animation-slide.modal-show {
  transform: translateY(0);
}

/* 动画 - 缩放 */
.base-modal-container.animation-zoom {
  transform: scale(0.7);
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.base-modal-container.animation-zoom.modal-show {
  transform: scale(1);
  opacity: 1;
}

/* 动画 - 无动画 */
.base-modal-container.animation-none {
  /* 无动画 */
}

/* 弹窗头部 */
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
  flex-shrink: 0;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
}

.close-btn {
  width: 56rpx;
  height: 56rpx;
  border-radius: 50%;
  background: #f5f5f5;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  flex-shrink: 0;
}

.close-btn:active {
  background: #e0e0e0;
  transform: scale(0.95);
}

.close-icon {
  font-size: 24rpx;
  color: #666;
  font-weight: bold;
}

/* 弹窗内容 */
.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 30rpx;
}

/* 弹窗底部 */
.modal-footer {
  display: flex;
  gap: 16rpx;
  padding: 24rpx 30rpx;
  border-top: 1rpx solid #f0f0f0;
  flex-shrink: 0;
}

.footer-btn {
  flex: 1;
  height: 80rpx;
  border-radius: 12rpx;
  font-size: 28rpx;
  font-weight: bold;
  border: none;
  transition: all 0.3s ease;
}

.cancel-btn {
  background: #f8f9fa;
  color: #636e72;
  border: 2rpx solid #e9ecef;
}

.cancel-btn:active {
  background: #e9ecef;
}

.confirm-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.confirm-btn:active {
  transform: scale(0.98);
}

/* 滚动条样式优化 */
.modal-body::-webkit-scrollbar {
  width: 6rpx;
}

.modal-body::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 3rpx;
}

.modal-body::-webkit-scrollbar-track {
  background: transparent;
}
</style> 